package cn.tianyigps.tianyigpsapiv2.oauth2.controller;

import cn.tianyigps.tianyigpsapiv2.oauth2.service.MongoClientDetailsService;
import cn.tianyigps.tianyigpsapiv2.param.enums.ResponseEnum;
import cn.tianyigps.tianyigpsapiv2.param.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * Created with IDEA
 * author:guibin.cui
 * Date:2018/12/12
 * Time:16:26
 */
@RestController
@RequestMapping("/client")
@CrossOrigin
@Api(value = "ClientDetailsController",description="客户接口")
public class ClientDetailsController {

    @Autowired
    private MongoClientDetailsService mongoClientDetailsService;
    @GetMapping("/{clientId}")
    @ApiOperation(value ="查询--根据clientId查询客户端信息",notes = "测试接口")
    public ResponseVo ClientDetails(@PathVariable String clientId) {
        ClientDetails clientDetails = mongoClientDetailsService.loadClientByClientId(clientId);
        if (!Optional.ofNullable(clientDetails).isPresent()){
            return ResponseVo.response(ResponseEnum.QUERY_NULL);
        }
        return ResponseVo.response(ResponseEnum.QUERY_SUCCESS,clientDetails);
    }

    @GetMapping
    @ApiOperation(value ="查询--查询客户端列表",notes = "测试接口")
    public ResponseVo listClientDetails() {

        List<ClientDetails> clientDetails = mongoClientDetailsService.listClientDetails();
        if (!Optional.ofNullable(clientDetails).isPresent()){
            return ResponseVo.response(ResponseEnum.QUERY_NULL);
        }
        return ResponseVo.response(ResponseEnum.QUERY_SUCCESS,clientDetails);
    }
    @PostMapping
    @ApiOperation(value ="新增--客户端",notes = "测试接口")
    public ResponseVo add(BaseClientDetails baseClientDetails) {

        ResponseVo ResponseVo = new ResponseVo();
        try {
            mongoClientDetailsService.addClientDetails(baseClientDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseVo.response(ResponseEnum.ADD_FAILURE);
        }
        return ResponseVo.response(ResponseEnum.ADD_SUCCESS,baseClientDetails);
    }

    @PutMapping
    @ApiOperation(value ="修改--客户端字段",notes = "测试接口")
    public ResponseVo update(BaseClientDetails baseClientDetails) {
        try {
            mongoClientDetailsService.updateClientDetails(baseClientDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseVo.response(ResponseEnum.UPDATE_FAILURE);
        }
        return ResponseVo.response(ResponseEnum.UPDATE_SUCCESS);
    }

    @DeleteMapping
    @ApiOperation(value ="修改--删除客户端",notes = "测试接口")
    public ResponseVo remove(String clientId) {
        try {
            mongoClientDetailsService.removeClientDetails(clientId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseVo.response(ResponseEnum.DELETE_FAILURE);
        }
        return ResponseVo.response(ResponseEnum.DELETE_SUCCESS);
    }

}
